package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class NursingProjectServiceImpl implements NursingProjectService{

    @Resource
    NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> query(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> nursingProjects = nursingProjectMapper.selectByPage(name, status);
        return PageResponse.of(nursingProjects,NursingProjectVo.class);
    }

    @Override
    public Boolean add(NursingProjectDto nursingProjectDTO) {
        //将DTO对象转化为实体对象
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);
        //插入数据
        int count = this.nursingProjectMapper.insert(nursingProject);
        return count > 0;
    }

    @Override
    public ResponseResult selectById(Integer id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        NursingProjectVo nursingProjectVo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return ResponseResult.success(nursingProjectVo);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return ResponseResult.success(nursingProjectMapper.deleteById(id));
    }

    @Override
    public ResponseResult updateStatus(Integer id, Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setStatus(status);
        UpdateWrapper<NursingProject> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        return ResponseResult.success(nursingProjectMapper.update(nursingProject, updateWrapper));
    }

    @Override
    public ResponseResult updateInfo(NursingProjectDto nursingProjectDTO) {
        NursingProject bean = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);
        return ResponseResult.success(nursingProjectMapper.updateById(bean));
    }

    @Override
    public ResponseResult listAll() {
        return ResponseResult.success(nursingProjectMapper.listAll());
    }


}
